package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import longcaisuyun.longcai.com.net.Post_goodsPay;
import longcaisuyun.longcai.com.net.Post_goodsPay.Info;
import longcaisuyun.longcai.com.utils.PhoneNumberUTils;
import longcaisuyun.longcai.com.view.EmojiEditText;


/**
 * Created by Administrator on 2016/5/28.
 */
public class ShangPinZhiFuActivity extends Activity {
    TextView tv_title;
    String title;
    String id;
    EmojiEditText et_name, et_phonenumber, et_address;
    EmojiEditText et_xinghaokuanshi, et_beizhu;
    Button bt_ok;
    LinearLayout ll_fanhui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shangpinzhifupage);
        View view = getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        tv_title = (TextView) findViewById(R.id.tv_title);
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        ll_fanhui = (LinearLayout) findViewById(R.id.fanhui_lin);
        bt_ok = (Button) findViewById(R.id.bt_ok);
        et_name = (EmojiEditText) findViewById(R.id.et_name);
        et_phonenumber = (EmojiEditText) findViewById(R.id.et_phonenumber);
        et_address = (EmojiEditText) findViewById(R.id.et_address);
        et_xinghaokuanshi = (EmojiEditText) findViewById(R.id.et_xinghaokuanshi);
        et_beizhu = (EmojiEditText) findViewById(R.id.et_beizhu);
        tv_title.setText(title);
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(et_name.getText().toString().trim())) {
                    Toast.makeText(ShangPinZhiFuActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_phonenumber.getText().toString().trim())) {
                    Toast.makeText(ShangPinZhiFuActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                } else if (!UtilMatches.checkMobile(et_phonenumber.getText().toString().trim())) {
                    Toast.makeText(ShangPinZhiFuActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_xinghaokuanshi.getText().toString().trim())) {
                    Toast.makeText(ShangPinZhiFuActivity.this, "型号及款式不能为空", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(et_address.getText().toString().trim())) {
                    Toast.makeText(ShangPinZhiFuActivity.this, "详细地址不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    getServerData();
                }


             /*   switch (isok()) {
                    case 1:

                        if (TextUtils.isEmpty(et_xinghaokuanshi.getText().toString().trim())) {
                            et_xinghaokuanshi.setText("用户未填写");
                        }
                        else if (TextUtils.isEmpty(et_beizhu.getText().toString().trim())) {
                            et_beizhu.setText("用户未填写");
                        }
                        else   if (!UtilMatches.checkMobile(et_phonenumber.getText().toString())){
                            Toast.makeText(ShangPinZhiFuActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                        }else {
                            getServerData();
                        }

                        break;
                    case 0:
                        Toast.makeText(ShangPinZhiFuActivity.this, "填写资料不完整，请检查后补充完整", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(ShangPinZhiFuActivity.this, "手机号码格式错误，请检查后重新填写", Toast.LENGTH_SHORT).show();
                        break;
                }
*/
            }
        });

    }

    public void getServerData() {


        new Post_goodsPay(et_name.getText().toString().trim(), et_phonenumber.getText().toString().trim(), et_address.getText().toString().trim(),
                id, MyApplication.myPreferences.readUid(), et_xinghaokuanshi.getText().toString().trim(), et_beizhu.getText().toString().trim(),
                new AsyCallBack<Info>() {
                    @Override
                    public void onSuccess(String toast, int type, Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        Log.i("et_name", et_beizhu.getText().toString().trim());
                        if (info.message.equals("1")) {
                            Toast.makeText(ShangPinZhiFuActivity.this, "您的联系方式已提交,稍后我们工作人员会联系您", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ShangPinZhiFuActivity.this, "提交不成功,请进入页面重新尝试", Toast.LENGTH_SHORT).show();
                        }
                        finish();
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);

                        // finish();
                    }
                }).execute(ShangPinZhiFuActivity.this);


    }

    public int isok() {
        if (et_name.getText().toString().trim().equals("") || et_phonenumber.getText().toString().trim().equals("")
                || et_address.getText().toString().trim().equals("")) {
            return 0;
        } else if (!PhoneNumberUTils.isMobileNumber(et_phonenumber.getText().toString().trim())) {
            return 2;
        } else {
            return 1;
        }

    }

}
